//author:kakake
/**
 * 支付系统对外提供接口
 * 
 */
class tsfrontController {
    public data = {
        dbConn: TSLobsterAPI.Program.GetDbConn(),
        WorkId: 1,
        AppId: 1
    }

    public service: tsfrontService;
    public dao: tsfrontDao;
    public weixinpayservice: weixinpayService;
    public cashworkservice: cashworkService;

    constructor() {
        this.data.WorkId = TSLobsterAPI.Program.GetWorkId();
        this.data.AppId = TSLobsterAPI.Program.GetAppId();

        this.service = new tsfrontService(this.data.WorkId);
        this.dao = new tsfrontDao(this.data.WorkId);
        this.cashworkservice = new cashworkService(this.data.WorkId, this.data.AppId);
        this.weixinpayservice = new weixinpayService(this.data.WorkId, this.data.AppId);
    }

    /**
     * 预下单
     * 参数：param
     * SubMchId: ret.SubMchId,//支付商户号
     * BusinessOrderNo: model.Id,//业务唯一标识
     * OrderType: 1,//业务类型 1预约挂号 2自助缴费
     * PayType: 1,//默认1 微信支付
     * PayScene: 1,//1 h5 2小程序 3app
     * Price: model.PayAmt * 100, //单位分
     * Body: '预约挂号',//商品描述
     * Memo: '备注'//可以不填
     * 
     * 返回：
     * pay.html 收银台页面
     */
    preorder() {
        // TSLobsterAPI.Utils.TrackLog('body',TSLobsterAPI.Program.RequestBody());
        var model = JSON.parse(TSLobsterAPI.Program.RequestBody());
        return this.cashworkservice.preorder(model);
    }

    /**
     * 调用微信支付
     * 参数：
     * orderNo: self.data.payOrderNo,
     * payType: payType
     * openid: self.data.openid, //微信openid
     */
    unifiedorder() {
        var model = JSON.parse(TSLobsterAPI.Program.RequestBody());
        return this.weixinpayservice.unifiedorder(model);
    }

    /**
     * 微信支付成功后回调通知接口
     * {
            subMchId = subMchId,  //支付接口传入的商户号
            orderNo = outTradeNo, //支付接口传入的订单号
            transactionId = tradeNo, //微信支付订单号
            totalFee = totalFee, //金额 单位：分
            bankType = bankType,  //付款银行
            timeEnd = timeEnd,
            attach = attach,
            mchId = mchId //微信商户号
        }
     */
    payNotify() {
        var model = JSON.parse(TSLobsterAPI.Program.RequestBody());
        this.weixinpayservice.payNotify(model);
    }

    /**
     * 关闭订单
     * {
            SubMchId: '20220505085609644323',
            BusinessOrderNo: "67"
        }
     */
    closeorder() {
        var model = JSON.parse(TSLobsterAPI.Program.RequestBody());
        return this.weixinpayservice.closeorder(model);
    }

    /**
     * 调用微信退款
     * {
            SubMchId: '20220505085609644323',
            BusinessOrderNo: "67",
            BusinessRefundNo: self.generateTimeReqestNumber(),
            PayType: 1,//默认1 微信支付
            RefundRealFee: 1, //单位分
            RefundDesc: '预约挂号退款',//商品描述 
        }
     */
    refund() {
        var model = JSON.parse(TSLobsterAPI.Program.RequestBody());
        return this.weixinpayservice.refund(model);
    }

    /**
     * 微信退款成功后回调通知接口
     * {
     *      OrderNo = outTradeNo,               //退款接口传入的原始订单号
            RefundNo = outRefundNo,             //退款接口传入的退款订单号 
            TransactionId = transaction_id,     //退款交易号
            RefundFee = refund_fee,
            TotalFee = total_fee,
            RefundStatus = refundStatus,        //1退款成功 2退款异常 3退款关闭
            SuccessTime = successTime,
            RefundId = refund_id,
            MchId = mch_id
        }
     */
    refundNotify() {
        var model = JSON.parse(TSLobsterAPI.Program.RequestBody());
        this.weixinpayservice.refundNotify(model);
    }


    /**
     * 根据订单号获取订单数据
     */
    getorder() {
        var orderNo = TSLobsterAPI.Program.RequestQuery("orderNo");
        var data = this.dao.getOrderByNo(orderNo);
        return { data: data };
    }
    
    /**
     * 获取支付方式
     */
    getpaytypelist() {
        var orderNo = TSLobsterAPI.Program.RequestQuery("orderNo");
        var data = this.dao.getPayTypeByNo(orderNo);
        return { data: data };
    }

    /**
     * 获取订单状态
     */
    getorderstate() {
        var orderNo = TSLobsterAPI.Program.RequestQuery("orderNo");
        var paystate = this.dao.getOrderStateByNo(orderNo);
        return { payState: paystate };
    }

    testOutService() {
        var data = { "OrderType": 2, "OrderNo": "20221221164929575499", "TransactionId": "4200001675202212210769569503", "BusinessOrderNo": "20221221164929207475", "SubMchId": "20220505085609644323", "PayDate": null, "PayType": 1, "PayFee": 4 };
        var res = TSLobsterAPI.AppFunction.CallOutService(this.data.AppId, "register.tspaynotify.paysuccess", data);
        TSLobsterAPI.Utils.TrackLog('register.tspaynotify.paysuccess', res);
        return res.data;
    }
}